package artibella.text 
{
	import artibella.loaders.LoaderMedia;
	import artibella.loaders.LoaderMediaParameters;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.ApplicationDomain;
	import flash.system.Capabilities;
	import flash.system.System;
	import flash.text.Font;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class FontsManager extends EventDispatcher
	{
		private static var _instance:FontsManager;
		
		private static var _tabFonts:Vector.<Font>
		private static var _tabItems:Vector.<LoaderMedia>;
		
		private var _itemsToLoad:int;
		
		public function FontsManager( c:PrivateClass = null )  
		{
			if( !c ) {
				throw new IllegalOperationError( 'Error : singleton pattern' );
			} else {

				_tabFonts = new Vector.<Font>();
				_tabItems = new Vector.<LoaderMedia>
			}
		}
		
		public static function get instance():FontsManager
		{
			return _instance || ( _instance = new FontsManager( new PrivateClass() ) );
		}
		
		// - - -
		
		public function addItem( url:String ):void
		{
			var params:LoaderMediaParameters = new LoaderMediaParameters();
			params.domained = new ApplicationDomain(ApplicationDomain.currentDomain);
			params.pfChecked = true;
			
			var item:LoaderMedia = new LoaderMedia( url, params, false );
			_tabItems.push( item );
		}
		
		public function load():void
		{
			for ( var i:int = 0; i < _tabItems.length; i ++ ) {
				
				var item:LoaderMedia = _tabItems[ i ] as LoaderMedia
				item.addEventListener( Event.COMPLETE, _onItemLoaded );
				item.load();
				
				_itemsToLoad ++;
			}
		}
		
		// - - -
		
		private function _onItemLoaded( e:Event ):void
		{
			e.target.removeEventListener( Event.COMPLETE, _onItemLoaded );
			
			_itemsToLoad --;
			
			if ( _itemsToLoad == 0 ) _onFontsLoaded( null );
			
		}
		
		private function _onFontsLoaded( e:Event = null ):void
		{
			var a:Array = Font.enumerateFonts( false ); // registered
			a.sortOn( "fontName" );
			
			trace( " " ); 
			trace( "--- F O N T S" ); 
			for ( var i:int = 0; i < a.length; i ++ ) {
				trace( a[ i ].fontName +  " ( " + a[ i ].fontStyle + " ) ");
				_tabFonts.push( a [ i ] );
			}
			
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
		
		
		
		
		///////////////////
		
		
		public static function getFontByName( name:String ):String 
		{
			//if ( name != "_sans") return name;
			//
			//trace( Capabilities.os );
			//
			//if( Capabilities.os.search( "Mac" ) != - 1 ){
//
				//var allFonts:Array = Font.enumerateFonts( true );
				//
				//for each ( var font:Font in allFonts )
				//{
					//if (font.fontName == "Helvetica Neue")
					//{
						// "Helvetica Neue" is there so use it instead of "_sans" ( bad on Mac )
						//return font.fontName;
					//}
				//}
			//}
			//
			
			for ( var i:int = 0; i < _tabFonts.length; i ++ ) {
				
				if ( _tabFonts[ i ].fontName == name )
				{
					return name;
				}
				
			}
				
				
			return "_sans";
		}
		
	}

}


/// - - -

class PrivateClass { }